/*ParallelPlatform Project - A web platform to distribute and share knowledge
Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.*/

$(document).ready(function(){
	
	$.each( $(".contentTemplate"), function(){
		var type = $(this).find("#id_type").val();
		var title = $(this).find("#id_title").val();
		var content = $(this).find("#id_content").val();
		$(this).empty();
		$(this).attr("pageType",type);
		$(this).append(jQuery('<header id="title">'+title+'</header>'+
							  '<div id="content">'+content+'</div>'));

	});
	
	loaderAloha(function(){
		
		/*$.each( $(".contentTemplate"), function(){
			$(this).find("#title").aloha();
			$(this).find("#content").aloha();
		});*/
		
		
		$(".descriptionTemplate").click(function(){
			var parent = $(this).parent();
			if( parent.attr("selectEditable") != true ){
				$(".itemTemplate").attr("selectEditable",false);
				parent.attr("selectEditable",true);
				$(".itemTemplate[selectEditable=false]").find(".contentTemplate > #title, .contentTemplate > #content").mahalo();
				$(".itemTemplate[selectEditable=true]").find(".contentTemplate > #title, .contentTemplate > #content").aloha();
				
				$(".itemTemplate[selectEditable=false]").find(".descriptionTemplate").removeClass("selectedBox").addClass("notselectedBox");
				$(".itemTemplate[selectEditable=true]").find(".descriptionTemplate").addClass("selectedBox").removeClass("notselectedBox");;
				
				$(".itemTemplate[selectEditable=false]").find(".contentTemplate").slideUp("fast");
				$(".itemTemplate[selectEditable=true]").find(".contentTemplate").slideDown("fast").addClass("selectedBox");
				
				//GENTICS.Aloha.Save.resetSave();
			
				GENTICS.Aloha.Save.newType = parent.find(".contentTemplate").attr("pageType");
				//GENTICS.Aloha.Save.addSave(parent.find("#title"));
				//GENTICS.Aloha.Save.addSave(parent.find("#content"));
				
			}
		});
		
		GENTICS.Aloha.Save.urlSave = "/"+document.URL.split("/")[3]+"/ajax/create/";
	});
	
});

